package com.zb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.SelectResultVo;
import com.zb.entity.SelectVo;
import com.zb.entity.ZjNetworkType;
import com.zb.mapper.ZjNetworkTypeMapper;
import com.zb.result.PageUtil;
import com.zb.service.ZjNetworkTypeService;
import com.zb.vo.NetworkTypeListVo;
import com.zb.vo.NetworkTypeVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网点类型表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-09-11
 */
@Service
public class ZjNetworkTypeServiceImpl extends ServiceImpl<ZjNetworkTypeMapper, ZjNetworkType> implements ZjNetworkTypeService {
    @Resource
    private ZjNetworkTypeMapper networkTypeMapper;

    // 新增或修改网点类型
    @Override
    public void save(Long custAccId, NetworkTypeVo networkTypeVo) {
        Assert.isTrue(null == networkTypeMapper.existByName(custAccId, networkTypeVo.getId(), networkTypeVo.getName()), "类型名称已重复！");
        ZjNetworkType type = new ZjNetworkType();
        BeanUtils.copyProperties(networkTypeVo, type);
        type.setDisabled(networkTypeVo.getStatus());
        if (null != type.getId()) {
            this.updateById(type);
            return;
        }
        type.setId(IdWorker.getId());
        type.setCustAccId(custAccId);
        this.save(type);
    }

    // 获取网点类型列表
    @Override
    public Map<String, Object> getList(Long custAccId, SelectVo selectVo) {
        QueryWrapper<ZjNetworkType> wrapper = new QueryWrapper<>();
        wrapper.eq("cust_acc_id", custAccId);
        if (StringUtils.isNotBlank(selectVo.getName())) {
            wrapper.like("name", selectVo.getName());
        }
        PageUtil.setPageLimit(selectVo);
        Page<ZjNetworkType> page = this.page(new Page<>(selectVo.getPage(), selectVo.getLimit()), wrapper);
        List<NetworkTypeListVo> voList = page.getRecords().stream().map(type -> {
            NetworkTypeListVo vo = new NetworkTypeListVo();
            BeanUtils.copyProperties(type, vo);
            vo.setStatus(type.getDisabled());
            vo.setCreateTime(type.getGmtCreate());
            vo.setUpdateTime(type.getGmtUpdate());
            return vo;
        }).toList();
        return PageUtil.pageList(page.getCurrent(), page.getSize(), page.getTotal(), voList);
    }

    // 根据类型ID获取网点类型详情
    @Override
    public NetworkTypeVo getDetail(Long id) {
        ZjNetworkType type = this.getById(id);
        Assert.isTrue(null != type, "类型参数[" + id + "]的数据不存在");

        NetworkTypeVo vo = new NetworkTypeVo();
        BeanUtils.copyProperties(type, vo);
        vo.setStatus(type.getDisabled());
        return vo;
    }

    // 批量启用或禁用类型
    @Override
    public void updateDisabledBatchById(List<Long> idList, boolean status) {
        networkTypeMapper.updateDisabledBatchById(idList, status);
    }

    // 分页获取网点类型下拉数据
    @Override
    public Map<String, Object> networkTypePageSelect(Long custAccId, SelectVo selectVo) {
        QueryWrapper<ZjNetworkType> wrapper = new QueryWrapper<>();
        wrapper.select("id", "name");
        wrapper.eq("cust_acc_id", custAccId).eq("is_disabled", false);
        if (StringUtils.isNotBlank(selectVo.getName())) {
            wrapper.like("name", selectVo.getName());
        }
        PageUtil.setSelectPageLimit(selectVo);
        Page<ZjNetworkType> page = this.page(new Page<>(selectVo.getPage(), selectVo.getLimit()), wrapper);
        List<SelectResultVo> voList = page.getRecords().stream().map(type -> {
            return new SelectResultVo(type.getId(), type.getName());
        }).toList();
        return PageUtil.pageList(page.getCurrent(), page.getSize(), page.getTotal(), voList);
    }
}
